import { Component, OnInit } from '@angular/core';
import { Location} from '@angular/common';
import { ProblemRelatedService} from '../problem-related.service';
import { Response} from '@angular/http';
import {GlobalService} from "../../global.service";
import {User} from "../../user/model/user-model";
import {Router} from "@angular/router";

@Component({
  selector: 'app-new',
  templateUrl: './new.component.html',
  styleUrls: ['./new.component.css']
})
export class NewSetsComponent implements OnInit {

  constructor(
    private location:Location,
    private problemRelatedService:ProblemRelatedService,
    private globalService:GlobalService,
    private router:Router,
  ) { }

  alertMessage:string = '练习期间不会隐藏其他题目集；练习不可提前交卷。';
  messages:['练习期间不会隐藏其他题目集；练习不可提前交卷。','考试期间将隐藏其他题目集；考试允许提前交卷。'];
  rangeDates:any;
  countM:number;//计算得到的分钟
  typeRadio:number = 1;
  timeRadio:string = 'fixed_duration';
  selectedValues: any;
  url:string = this.location.path();
  categoryCode:string;
  newSet:NewSet = new NewSet;
  user:User = new User;
  btnLabel:any;
  api:any;

  ngOnInit() {
    this.api = this.globalService.api;
    this.setCode = '-1';
    this.getCategoryCode();
    this.getSetCode();
    //初始化
    this.newSet.type = 1;

    this.user = this.globalService.getCurrentUser();

    if (this.updateFlag){
      this.btnLabel = "更新";
      this.setSetValue();
    }else {
      this.btnLabel = "新建";
    }

  }

  clickRadio(event){
    let value = event.target.value;
    if (value=='exercise'){
      this.alertMessage = '练习期间不会隐藏其他题目集；练习不可提前交卷。';
      this.typeRadio = 1;
      this.newSet.type = 1;
    }else if (value == 'exam'){
      this.alertMessage = '考试期间将隐藏其他题目集；考试允许提前交卷。';
      this.typeRadio = 0;
      this.newSet.type = 0;
    }
  }

  getCategoryCode(){
    let url = this.url;
    let start = this.url.lastIndexOf('newCategory/') + 12;
    if (start!=11){
      this.categoryCode = url.slice(start,url.length);
      console.log("categoryCode:"+this.categoryCode);
    }
  }
  setCode:any;
  updateFlag:boolean = false;
  getSetCode(){
    let url = this.url;
    let start = this.url.lastIndexOf('update/') + 7;
    if(start!=6){
      this.updateFlag = true;
      let url2 = url.slice(start,url.length);
      let start2 = url2.lastIndexOf('/');
      this.categoryCode = url2.slice(0,start2);
      this.setCode = url2.slice(start2+1,url2.length);
      console.log("setCode:"+this.setCode);
    }
  }

  //判断是否选择了时间限制
  judgeTime(){

  }

  selectTimeFlag:boolean = true;
  selectTime(event){
    console.log("rangeDates");
    console.log(this.rangeDates);
    let temp = this.rangeDates;
    if (temp[1]!=null){
      console.log("不为空");
      let end = new Date(temp[1]);
      let start = new Date(temp[0]);
      this.countM = (end.getTime()-start.getTime())/60000;
      this.selectTimeFlag  = false;
    }
  }

  clickTimeRadio(event){
    let value = event.target.value;
    console.log(value);
    this.timeRadio = value;
  }

  /**
   * 更新赋初值
   */
  setSetValue(){
    this.problemRelatedService.getSet(this.setCode)
      .subscribe((res:Response)=>{
        let data = res.json();
        if(data.success){
          console.log("set:",data.value);
          this.newSet.name = data.value.name;
          this.newSet.type = data.value.type;
          this.newSet.notice  =data.value.notice;
          this.rangeDates = [];
          this.rangeDates.push(new Date(data.value.startTime));
          this.rangeDates.push(new Date(data.value.endTime));
          this.selectedValues = data.value.originCode.split(',');
        }
      });
  }

  /**
   * 创建新题目集
   */

  saveNewSet(){
    console.log("标题:");
    if (this.newSet.name==null||this.newSet.name==''){
      alert("请先输入标题!");
      return;
    }
    console.log(this.newSet.name);
    console.log("类型:");
    console.log(this.newSet.type);
    console.log("时间类型:");
    console.log(this.timeRadio);
    if (this.timeRadio == 'fixed_duration'){
      this.newSet.timeType = 0;
    }else if (this.timeRadio == 'fixed_time'){
      this.newSet.timeType = 1;
    }
    console.log("开始时间和结束时间:");
    console.log(this.rangeDates);
    if (this.rangeDates!=null&&this.rangeDates.length>=2){
      this.newSet.startTime = this.rangeDates[0].getTime();
      this.newSet.endTime = this.rangeDates[1].getTime();
    }else {
      alert("请选择开始和结束的时间!");
      return;
      // this.newSet.startTime = new Date;
      // this.newSet.endTime = new Date;
    }
    console.log("编译器限制:");
    if (this.selectedValues==null){
      alert("请选择开始和结束时间!");
      return;
    }
    console.log(this.selectedValues);
    this.newSet.originCode = '';
    for(let index of this.selectedValues){
      if (index!=""){
        this.newSet.originCode = this.newSet.originCode + index + ',';
      }
    }
    console.log("公告:");
    console.log(this.newSet.notice);
    console.log("当前用户",this.user.code);
    this.newSet.creator = this.user.code;
    //设置题目集是否公开
    if (this.user.role=='admin'){
      this.newSet.open = 1;
    }else {
      this.newSet.open = 0;
    }

    console.log("结果:");
    console.log(this.newSet);
    let problemCategory = new ProblemCatogery();
    problemCategory.code = this.categoryCode;
    problemCategory.problemSets = [];
    problemCategory.problemSets[0] = this.newSet;

    if (this.updateFlag){
      this.newSet.code = this.setCode;
      this.newSet.properties = {update:true};
      this.problemRelatedService.updateSet(this.newSet)
        .subscribe((res:Response)=>{
          console.log("请求完成.");
          this.router.navigate(['problem-sets']);
        });
    }else {
      this.problemRelatedService.createSet(problemCategory)
        .subscribe((res:Response)=>{
          console.log("请求完成.");
          this.router.navigate(['problem-sets']);
        });
    }
  }

}

export class ProblemCatogery{
  code:string;
  problemSets:any;
}

export class NewSet{
  code:string;
  name:string;
  type:number;
  timeType:number;
  startTime:Date;
  endTime:Date;
  originCode:string;
  notice:string;
  creator:string;
  open:number;
  properties:any;
}
